package com.hilaver.dzmis.exhibition.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.exhibition.hibernate.ExOrder;
import com.hilaver.dzmis.exhibition.hibernate.ExOrderItem;
import com.hilaver.dzmis.exhibition.hibernate.TechnicChart;
import com.hilaver.dzmis.exhibition.hibernate.TechnicChartMaterial;
import com.hilaver.dzmis.exhibition.hibernate.Yarn;
import com.hilaver.dzmis.exhibition.service.OrderService;
import com.hilaver.dzmis.exhibition.service.TechnicChartService;
import com.hilaver.dzmis.exhibition.utils.Constants;
import com.hilaver.dzmis.exhibition.utils.StringUtils;

/**
 * Servlet implementation class for Servlet: YarnServlet
 * 
 */
public class OrderServlet extends AbstractBaseServlet {
	static final long serialVersionUID = 1L;

	public final static int ACTION_CONFIRM = 12;
	// description";

	public final static int ACTION_PRINT = 13;

	public final static int ACTION_EDIT_ORDER_ITEM = 14;

	public final static int ACTION_GET_ITEM_LIST = 15;

	public final static int ACTION_GET_ORDER_ITEM = 16;

	public final static int ACTION_DELETE_ORDER_ITEM = 17;

	private Integer tcId;

	private ExOrder order;

	private ExOrderItem orderItem;

	private OrderService serviceObj;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public OrderServlet() {
		this.fullName = ExOrder.class.getName();
		this.simpleName = ExOrder.class.getSimpleName();
		this.serviceObj = new OrderService();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);

		Method method = null;
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAllPagination",
					new Class[] { int.class, String.class, String.class });// int
			// page,
			// String
			// sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir });
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			method = serviceObj.getClass().getMethod("edit",
					new Class[] { ExOrder.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.obj });
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			method = serviceObj.getClass().getMethod("delete",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			method = serviceObj.getClass().getMethod("get",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });// int page, String sort, String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			break;
		case ACTION_GET_ALL:
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAll",
					new Class[] { String.class, String.class });// String sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.sort,
					this.dir });
			break;
		case ACTION_CONFIRM:
			this.id = Integer.parseInt(request.getParameter("id"));
			this.order = this.serviceObj.getObj(this.id);
			this.order.setIsConfirmed("1");
			this.serviceObj.edit(order);
			rtn = "success";
			break;

		case ACTION_PRINT:
			this.id = Integer.parseInt(request.getParameter("id"));
			this.order = this.serviceObj.getObj(this.id);
			this.order.setIsPrinted("1");
			this.serviceObj.edit(order);
			rtn = "success";
			break;
		case ACTION_EDIT_ORDER_ITEM:
			this.serviceObj = new OrderService();
			this.fillItemObj(request);
			rtn = this.serviceObj.editItem(this.orderItem);
			break;
		case ACTION_GET_ITEM_LIST:
			super.setId(request);
			rtn = this.serviceObj.getAllItem(this.id);
			break;
		case ACTION_GET_ORDER_ITEM:
			super.setId(request);
			rtn = this.serviceObj.getItem(this.id);
			break;
		case ACTION_DELETE_ORDER_ITEM:
			super.setId(request);
			rtn = this.serviceObj.deleteItem(this.id);
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.order = (ExOrder) super.obj;
		this.order.setIsConfirmed("0");
		this.order.setIsPrinted("0");
	}

	protected void fillItemObj(HttpServletRequest request) throws Exception {
		super.setId(request);
		this.orderItem = new ExOrderItem();
		this.orderItem.setId(this.id);
		this.order = new ExOrder();
		this.order.setId(Integer.parseInt(request.getParameter("orderId")));
		this.orderItem.setExOrder(this.order);

		TechnicChartMaterial tcm = new TechnicChartMaterial();
		tcm.setId(Integer.parseInt(request.getParameter("tcmId")));

		this.orderItem.setTechnicChartMaterial(tcm);
		this.orderItem.setColor(request.getParameter("color"));

		this.orderItem.setQuantity(Double.parseDouble(request
				.getParameter("quantity")));

		this.orderItem.setTotalPrice(Double.parseDouble(request
				.getParameter("totalPrice")));
		this.orderItem.setCo(Double.parseDouble(request.getParameter("coefficient")));
		
	}
}